/**
 * Created by Mark_ren on 2016/11/28.
 * 苟富贵,無相忘
 */
var moment = require("moment");
var co = require('co');
const _ = require('lodash');
var util = require('../../../common/util');
var db = require('../../../models/index');
var userController = {
    getUserPage: function (req, res) {
        co(function () {
            var page = parseInt(req.query.page);
            var params = {};
            var paramT = {};
            if (!_.isEmpty(req.query)) {
                var query = req.query;
                if (_.hasIn(query, 'user_name')) {
                    params["user_name"] = { "$like": "%" + req.query.user_name + "%" };
                }
                if (_.hasIn(query, 'page') && _.hasIn(query, 'per_page')) {
                    paramT["offset"] = parseInt(req.query.page);
                    paramT["limit"] = parseInt(req.query.per_page);
                }
                if (_.hasIn(query, 'order') && _.hasIn(query, 'by')) {
                    paramT.order = [[query.order, query.by]]
                }
            }
            paramT["where"] = params;
            paramT.attributes = { exclude: ['password'] }
            db.user.findAndCountAll(paramT).then(function (result) {
                result.page = page;
                res.status(200).json(result);
            });
        }).catch(function (e) {
            res.status(500).json({ message: '服务错误' })
            console.log(e);
        })
    },
    getUserById: function (req, res) {
        co(function () {
            if (_.hasIn(req.params, 'id')) {
                db.user.findOne({ where: req.params }).then(function (result) {
                    res.status(200).json(result);
                });
            } else {
                res.status(403).json({ message: "参数不对" });
            }
        }).catch(function (e) {
            res.status(500).json({ message: '服务错误' })
            console.log(e);
        })
    },
    addUser: function (req, res) {
        co(function () {
            req.body.password = util.MD5(req.body.password);
            db.user.create(req.body).then(function (result) {
                res.status(200).json(result);
            });
        }).catch(function (e) {
            res.status(500).json({ message: '服务错误' })
            console.log(e);
        })
    },
    updateUser: function (req, res) {
        co(function () {
            if (!_.hasIn(req.params, 'id')) {
                res.status(403).json({ message: '参数错误' })
                return;
            }
            db.user.update(req.body, { where: req.params }).then(function (result) {
                res.status(200).json(result);
            });
        }).catch(function (e) {
            res.status(500).json({ message: '服务错误' })
            console.log(e);
        })
    },
    deleteUser: function (req, res) {
        co(function () {
            if (_.hasIn(req.query, 'id')) {
                db.user.destroy({ where: req.query }).then(function (result) {
                    res.status(200).json(result);
                });
            } else {
                res.status(403).json({ message: "参数不对" });
            }
        }).catch(function (e) {
            res.status(500).json({ message: '服务错误' })
            console.log(e);
        })
    }
}
module.exports = userController;